package top.pengdong.pictureShare.userAuth.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.pengdong.pictureShare.common.model.R;
import top.pengdong.pictureShare.userAuth.entity.TAuthEntity;
import top.pengdong.pictureShare.userAuth.entity.TRoleEntity;
import top.pengdong.pictureShare.userAuth.service.TAuthService;
import top.pengdong.pictureShare.userAuth.service.TRoleService;
import top.pengdong.pictureShare.userAuth.service.TUsersService;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @projectName: pictureShare
 * @see: top.pengdong.pictureShare.userAuth.controller
 * @author: pc
 * @createTime: 2022/5/1 23:09
 * @version: 1.0
 */
@RestController
@RequestMapping("api/admin")
public class AdminController {

	@Autowired
	private TUsersService tUsersService;

	@Autowired
	private TAuthService tAuthService;

	@Autowired
	private TRoleService tRoleService;

	@RequestMapping("/login")
	public R login() {
		return R.ok("管理员登陆成功");
	}

	/**
	 * 获取菜单列表
	 * @return
	 */
	@GetMapping("/menu")
	public R getMenu() {
		return tUsersService.getMenu(1);
	}


	/**
	 * 删除菜单
	 * @param id
	 * @return
	 */
	@GetMapping("/delete_menu")
	public R deleteMenu(@RequestParam Integer id) {
		tAuthService.update(new UpdateWrapper<TAuthEntity>().eq("id", id).set("status", 0));
		return R.ok("删除成功");
	}

	/**
	 * 添加菜单表单
	 * @param tAuthEntity
	 * @return
	 */
	@PostMapping("/add_menu")
	public R addMenu(@RequestBody TAuthEntity tAuthEntity) {
		tAuthService.save(tAuthEntity);
		return R.ok("添加成功");
	}


	@GetMapping("/role")
	public R getRoles() {
		List<String> list = tRoleService.list().stream().map(
				role -> {
					return role.getName();
				}
		).collect(Collectors.toList());
		return R.ok("返回角色信息").put("result", list);
	}
}
